package com.ttbj.util;

import java.io.Serializable;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.StringUtils;

import com.ttbj.constant.FlagEnum;
import com.ttbj.exception.WebException;
import com.ttbj.product.bean.dto.ViewData;

public class ResultUtil implements Serializable {

	private static final long serialVersionUID = 3004141835216050420L;

	/**
	 * 创建错误结果
	 * 
	 * @param MESSAGE
	 * @return
	 */
	public static ViewData createFail(String fileName, int messageCode, Object ... objs) {
		String message = null;
		if (objs == null) {
			message = ResourcesUtil.getValue(fileName, messageCode + "");
		} else {
			message = ResourcesUtil.getValue(fileName, messageCode + "", objs);
		}
		return new ViewData(messageCode, message, null);
	}
	/**
	 * 创建正确
	 * @param MESSAGE
	 * @return
	 */
	public static ViewData createSuccess(String fileName, int messageCode, Object[] objs, T data) {
		String message = null;
		if (objs == null) {
			message = ResourcesUtil.getValue(fileName, messageCode + "");
		} else {
			message = ResourcesUtil.getValue(fileName, messageCode + "", objs);
		}
		return new ViewData(FlagEnum.SUCCESS.code(), message, data);
	}
  
	/**
	 * 创建参数缺少的提示
	 */
	public static ViewData createWarning(String fileName, int messageCode, Object ...objs) {
		String message = null;
		if (objs == null) {
			message = ResourcesUtil.getValue(fileName, messageCode + "");
		} else {
			message = ResourcesUtil.getValue(fileName, messageCode + "", objs);
		}
		return new ViewData(messageCode, message, null);
	}

	/**
	 * 抛出异常
	 * 
	 * @param resultInfo
	 * @throws ExceptionResultInfo
	 */
	public static void throwExcepion(ViewData model) throws WebException {
		throw new WebException(model);
	}

	/**
	 * 抛出带有特殊提示信息的异常
	 * @param resultInfo
	 * @throws ExceptionResultInfo
	 */
	public static void throwExcepion(ViewData model, String errorMssage) throws WebException {
		if (StringUtils.isEmpty(errorMssage)) {
			throwExcepion(model);
		}
		throw new WebException(model, errorMssage);
	}

}
